package com.yenlien.traceyun.plant.controller;

import com.yenlien.traceyun.plant.dao.OrderDetailedDao;
import com.yenlien.traceyun.plant.entity.Order;
import com.yenlien.traceyun.plant.service.OrderService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * Created by LiuSha on 2020/7/28.
 * 订单管理
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailedDao orderDetailedDao;

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody Order order) {
        Page<Order> page = orderService.findByList(order);
        if (page.getContent().size() > 0) {
            for (Order order1 : page.getContent()) {
                order1.setOrderDetailedList(orderDetailedDao.findByOrderId(order1.getId()));
            }
        }
        return buildPage(page);
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody Order order) {
        orderService.save(order);
        return success("success");
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@RequestBody Order order) {
        orderService.update(order);
        return success("success");
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        return success(orderService.findById(id));
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Object delete(@PathVariable String id) {
        orderService.delete(id);
        return success("success");
    }

}
